/*
 * main.cpp
 *
 *  Created on: 2011-8-5
 *      Author: minorityee1984@gmail.com
 *     Purpose:
 *        Note:
 */

#include "mainserver.h"
#include "logengine/simplelog.h"
#include <signal.h>

static void hook_sig();
static void reload();
static void terminate();

int main(int argc, char** argv)
{
	hook_sig();
	int32_t ret = S_OK;
	CMainServer &mainServer = CREATE_MAINSERVER_INSTANCE();
	ret = mainServer.Initialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "mainServer init failed! ret=0x%08X", ret);
		return ret;
	}

	mainServer.Run();

	ret = mainServer.Uninitialize();
	if (0 > ret)
	{
		WRITE_SERVER_LOG(enmLogLevel_ERROR, "mainServer uninit failed! ret=0x%08X", ret);
		return ret;
	}

	return S_OK;
}

void hook_sig()
{
	signal(SIGHUP, SIG_IGN);
	signal(SIGPIPE, SIG_IGN);

	signal(SIGUSR1, reload);
	signal(SIGUSR2, terminate);
}

void reload()
{
	GET_MAINSERVER_INSTANCE().Reload();
}

void terminate()
{
	GET_MAINSERVER_INSTANCE().Terminate();
}
